package com.zabra.zl.ui.mybank;

import android.app.Dialog;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.zabra.zl.Config;
import com.zabra.zl.R;
import com.zabra.zl.base.BaseMvpActivity;
import com.zabra.zl.data.BankListBean;
import com.zabra.zl.data.EventMessage;
import com.zabra.zl.ui.addbank.AddBankActivity;
import com.zabra.zl.ui.detailsbank.DetailsBankActivity;
import com.zabra.zl.ui.mybank.contract.MyBankContract;
import com.zabra.zl.ui.mybank.presenter.MyBankPresenter;
import com.zabra.zl.utils.DialogUtils;
import com.zabra.zl.utils.MaptoJsonUtils;
import com.zabra.zl.utils.SharedPreferencesUtil;
import com.zabra.zl.utils.ShowDialogUtils;
import com.zabra.zl.utils.ToastUtil;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MyBankActivity extends BaseMvpActivity<MyBankPresenter> implements MyBankContract.View {


    @BindView(R.id.back)
    LinearLayout back;
    @BindView(R.id.tv_addbank)
    TextView tvAddbank;
    @BindView(R.id.recycler)
    RecyclerView recycler;
    private List<BankListBean.CardsBean> list;
    private Adp adp;
    private Dialog dialog;
    private Bundle bundle=new Bundle();
    private String strUserName,strIDcard;
    @Override
    public int getLayoutId() {
        return R.layout.activity_my_bank;
    }


    @Override
    public void initView() {
        strUserName = SharedPreferencesUtil.getInstance(this).getSP("username");
        strIDcard = SharedPreferencesUtil.getInstance(this).getSP("idcard");
        mPresenter = new MyBankPresenter();
        mPresenter.attachView(this);
        keyList.add("debtorId");
        valueList.add(strUserid);
        mPresenter.getData(MaptoJsonUtils.Simple(keyList, valueList));


    }


    @Override
    public void showLoading() {
        dialog=DialogUtils.showDialog_progressbar(this);
    }

    @Override
    public void hideLoading() {
        DialogUtils.closeDialog(dialog);

    }

    @Override
    public void onError(Throwable throwable) {
        DialogUtils.closeDialog(dialog);
    }

    @Override
    public void onSuccess(BankListBean bean, String token) {
        if (bean.getRespCode().equals(Config.SUCCESS)){
            SharedPreferencesUtil.getInstance(this).putSP("token",token);
            list=bean.getCards();
            recycler.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
            adp = new Adp(R.layout.item_bank, list);
            recycler.setAdapter(adp);
            adp.openLoadAnimation();
            adp.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    //详情
                    bundle.putString("bank",list.get(position).getBankName());
                    bundle.putString("bankid",list.get(position).getCardNo());
                    bundle.putString("cardid",list.get(position).getCardId()+"");
                    toClass1(view.getContext(), DetailsBankActivity.class,bundle);
                }
            });
        }else if (bean.getRespCode().equals(Config.TOKENOUT)){
            ShowDialogUtils.restLoginDialog(this);
        }else {
            ToastUtil.showToast(this,bean.getRespDesc());
        }

    }


    @OnClick({R.id.back, R.id.tv_addbank})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.tv_addbank:
                bundle.putString("dealpwd","yes");
                toClass1(this, AddBankActivity.class,bundle);
                EventMessage message = new EventMessage(1, "MyBank");
                EventBus.getDefault().postSticky(message);
                break;
        }
    }
}
